package com.stone.business.jc.action;

/**
 * <pre>
 * 考勤
 * </pre>
 */

import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.jc.pojo.TTx;
import com.stone.business.jc.service.JcCommonService;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TUser;
import com.stone.utils.JsonUtils;

import frm.ssh.action.BaseListAction;
import frm.ssh.exception.AppException;
import frm.ssh.util.page.CustomPage;

public class TxAction extends BaseListAction {

	private static final long serialVersionUID = 3979577965804736350L;
	
	private String start;
	private String limit;
	
	private String userid;
	String bzReadOnly;
	String cjReadOnly;
	private String id;

	private JcCommonService myService;
	
	private TTx entity;

	public void setMyService(JcCommonService myService) {
		this.service = myService;
		this.myService = myService;
	}
	
    /** * 构造方法. */
    public TxAction() {
        this.entityClass = TTx.class;
    }
    
    /**
     * 初始化进入
     */
    public String initDetail() throws Exception {
    	TUser user = (TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER);
		String roles = (String) super.getSession().getAttribute(IConstant.SESSION_ROLES);
		System.out.println("roles=="+roles);
		 if (roles.contains("ZBZ")) {
			 bzReadOnly="false";
		 }else{
			 bzReadOnly="true";
		 }
		 if (roles.contains("gqgl")) {
			 cjReadOnly="false";
		 }else{
			 cjReadOnly="true";
		 }
		 if (roles.contains("isadmin")) {
			 bzReadOnly="false";
			 cjReadOnly="false";
		 }
		 super.getSession().setAttribute("bzReadOnly", bzReadOnly);
		 super.getSession().setAttribute("cjReadOnly", cjReadOnly);
    	// 调休日期 默认系统日期当天
    	List<HashMap<String,Object>> list = myService.findListBySql("SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD') AS CSRQ  FROM DUAL");
    	
		super.getSession().setAttribute("CSRQ", list.get(0).get("csrq"));
		
		return SUCCESS;
    }
    
    /**
     * 保存
     * @throws Exception
     */
    public void save() throws Exception {
    	
		// 修改直接修改
		if (entity.getId() != null && entity.getId() != 0) {
			
			// 是否修改为其他已有的日起
			Long count = myService.getTotalByDHql("ksrq = ? and userid = ? and id != ?", new Object[]{entity.getKsrq(), entity.getUserid(),entity.getId()}, "TTx");
			
			if (count == 0) {
				myService.update(entity);
			}
				
			// 新建要判断字典是否重复
		} else {
		
			// 判断是否已经有了 字典类型
			Long count = myService.getTotalByDHql("ksrq = ? and userid = ? ", new Object[]{entity.getKsrq(), entity.getUserid()}, "TTx");
			
			if (count > 0 ) {
				
				myService.updateByHQL("update TTx set qjts = ? , lut = ? where ksrq = ? and userid = ? ", new Object[]{entity.getQjts(), entity.getLut(), entity.getKsrq(), entity.getUserid()});
			} else {

				myService.save(entity);
			}
			
		}
    	
        super.getResponse().getWriter().print("{msg:'保存成功'}");
    	
    }
    
    /**
     * 删除
     * @throws Exception
     */
    public void delete() throws Exception {
    	
    	myService.delete(Long.parseLong(id), "TTx");
    	
        super.getResponse().getWriter().print("{msg:'删除成功'}");
    	
    }
    
	/**
	 * 用户的出勤
	 * 
	 * @return
	 * @throws AppException
	 */
	public void getListExt() throws Exception {

		super.setPage((String.valueOf((Integer.parseInt(getStart()) / Integer.parseInt(getLimit()) + 1))));
		
		HashMap<String, Object> m = new HashMap<String, Object>();
		
		String cols = " kq.id as id, kq.userid as userid,kq.organid as organid,kq.ksrq as ksrq, kq.qjts as qjts,jb as jb,jsrq as jsrq,bzname as bzname,cjname as cjname,shrq as shrq,pzrq as pzrq,bz as bz, kq.sy as sy,lut as lut";

		m.put("cols", cols);
		m.put("alias", "kq");
		m.put("join", "");
	
		String conditions = " kq.userid = '" + userid + "'";// '"+super.getSearchBean().getCon1()+"'");

		m.put("conditions",conditions);//conditions);
		m.put("clause", " order by kq.ksrq desc");
		
		// 根据起始记录查询当前页记录
		List<HashMap<String,Object>> list = myService.getPageListByDHql(Integer.parseInt(getStart()), Integer.parseInt(getLimit()) , m,this.entityClass.getName() );
		
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON(list, jsonConfig);

		String strTree = "{result:" + json.toString() + ",totalCount:[" + service.getTotalByDHql(conditions, null, this.entityClass.getName() + " as kq"  ) + "]}";
		
        super.getResponse().getWriter().print(strTree);
		
	}

	public String getStart() {
		return start == null ? "0" : start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getLimit() {
		return limit == null ? String.valueOf(CustomPage.PER_PAGE) : limit;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	@Override
	public HashMap<String, Object> setSearchCondition() {
		// TODO Auto-generated method stub
		return null;
	}

	public TTx getEntity() {
		return entity;
	}

	public void setEntity(TTx entity) {
		this.entity = entity;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getBzReadOnly() {
		return bzReadOnly;
	}

	public void setBzReadOnly(String bzReadOnly) {
		this.bzReadOnly = bzReadOnly;
	}

	public String getCjReadOnly() {
		return cjReadOnly;
	}

	public void setCjReadOnly(String cjReadOnly) {
		this.cjReadOnly = cjReadOnly;
	}

	
	
}
